<?php
$bartUserServices = new BartuserService();
$orderServices = new OrdersService();
$orderDetailTemp = new OrderDetailstempService();
$tool = new Tool();
if(isset($_GET['opt']))
{
	$opt = $_GET['opt'];
	$moduleID = $moduleService->getModuleIDByFuncAndOpt($context, $result, $func, $opt);
	$permit = $permitService->checkPermition($context, $result, $_SESSION['user']['type'], $moduleID);

	if($opt == "PIList" && $permit ==true)
	{
		$smarty->assign('tool', $tool);	
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countByStatus($context, $result, "2,3");// 2 is status PO, 3 is PI
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllOrderDisplayByTypeByRange($context, $result, "2,3", $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listPerformaInvoice.tpl");
			
	}
	
	if($opt == "listPICustomer" && $permit ==true)
	{			
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countAllOrderbyConsigneeByStatus($context, $result,$_SESSION['user']['ID'], "3");
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllOrderByConsigneeByStatus($context, $result, $start, $configPage['limit'], $_SESSION['user']['ID'], "3");
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listProformaInvoiceCus.tpl");
	}
	if($opt == "listPIFactory" && $permit ==true)
	{			
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countAllOrderbyFactoryByStatus($context, $result,$_SESSION['user']['ID'], "3,4");
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllOrderByFactoryByStatus($context, $result, $start, $configPage['limit'], $_SESSION['user']['ID'], "3,4");
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listProformaInvoiceFact.tpl");	
	}
	if($opt == "searchPICus" && $permit ==true)
	{
		if(isset($_POST['dateFrom']) && isset($_POST['dateTo']) && $_POST['dateFrom'] != '' && $_POST['dateTo'] != '')
		{
			$dateFrom = $_POST['dateFrom'];
			$dateTo = $_POST['dateTo'];
		}
		else {
			$dateFrom = '0000-00-00';
			$dateTo = date('Y-m-d', time());
		}
		$consigneeID = $_SESSION['user']['ID'];
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countAllByStatusByConsigneeOrderDate($context, $result, "3", $consigneeID, $dateFrom, $dateTo);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);

		$orderList = $orderServices->findAllByStatusByConsigneeOrderDateByRange($context, $result, "3", $start, $configPage['limit'], $consigneeID, $dateFrom, $dateTo);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";

		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listProformaInvoiceCus.tpl");
			
	}
	
	if($opt == "searchPIFact" && $permit ==true)
	{
		if(isset($_POST['dateFrom']) && isset($_POST['dateTo']) && $_POST['dateFrom'] != '' && $_POST['dateTo'] != '')
		{
			$dateFrom = $_POST['dateFrom'];
			$dateTo = $_POST['dateTo'];
		}
		else {
			$dateFrom = '0000-00-00';
			$dateTo = date('Y-m-d', time());
		}
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countAllOrderbyFactoryByStatus($context, $result,$_SESSION['user']['ID'], "3,4", $dateFrom, $dateTo);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->getAllOrderByFactoryByStatusOrderDate($context, $result, $start, $configPage['limit'], $_SESSION['user']['ID'], "3,4", $dateFrom, $dateTo);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listProformaInvoiceFact.tpl");	
			
	}
	
	if($opt == "viewPIList" && $permit ==true)// missing permit
	{
		if(isset($_GET['pid']) && is_numeric($_GET['pid']))
		{
			$proformaID = $_GET['pid'];
			$proformaOrder = $orderServices->getOrderByOrderID($context, $result, $proformaID);
			$start=$p->findStart($configPage['limit']);
			$count=$proformaOrder->getOrdersDomain()->getFactNum();
			$pages=$p->findPages($count,$configPage['limit']);
			$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			
			$orderList = $orderServices->getPurchaseOrderArr($context, $result, $proformaID);
			$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
			$smarty->assign('total', $total);
			$smarty->assign('page_list', $page_list);
			$smarty->assign('orderList', $orderList);
			
			$smarty->display($templateDir . "/temp1/listProformaInvoiceDetails.tpl");
		}
		else
		{
			header("Location:index.php?func=dash");
		}		
	}

	if($opt == "add" && $permit ==true)
	{
		$orderDetailArr = array();
		if(isset($_SESSION['orderDetail']))
		{
			foreach ($_SESSION['orderDetail'] as $orderDetail)
			{
				$orderDetailArr[] = unserialize($orderDetail);
			}


			$smarty->assign('orderDetailArr', $orderDetailArr);
		}
			
		$bArtUser = $bartUserServices->getBArtUserByOid($context, $result, $_SESSION['user']['ID']);
			
		$smarty->assign('bartUser', $bArtUser);
		$smarty->display($templateDir . "/temp1/addPI.tpl");
	}

	if($opt == "edit" && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$smarty->display($templateDir . "/temp1/editOrder.tpl");
		}
	}
	if($opt == 'viewPI' && $permit ==true)
	{
		$order = $orderServices->getPurchaseOrderByPurchaseIDByFactoryID($context, $result, $_GET['id'], $_SESSION['user']['ID']);
			
		$type = $order->getOrdersDomain()->getType();

		$orderDetailArr = $order->getOrderDetail();
		if(!Tool::isEmpty($orderDetailArr))
		{
			$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
		}
		$smarty->assign('order', $order);
					
		$smarty->display($templateDir . "/temp1/viewOrderPI.tpl");
	}
	
	if($opt == 'viewPICus' && $permit ==true)
	{
		$order = $orderServices->getOrderByConsigneeByStatus($context, $result, $_GET['pid'], $_SESSION['user']['ID'],"3");
		$orderDetailArr = $order->getOrderDetail();
		if(!Tool::isEmpty($orderDetailArr))
		{
			$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
		}
		$smarty->assign('order', $order);
		
		$smarty->assign('tool', $tool);		
		$smarty->display($templateDir . "/temp1/viewOrderPI.tpl");
	}
	
	if($opt == 'viewPInvoiceBArt' && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']) && isset($_GET['fid']) && is_numeric($_GET['fid']))
		{
			$order = $orderServices->getPurchaseOrderByPurchaseIDByFactoryID($context, $result, $_GET['id'], $_GET['fid']);
			$orderDetailArr = $order->getOrderDetail();
			$stt = $orderDetailArr[0]->getOrderdetailsDomain()->getStt();
			if($stt == 1)
			{
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign('tool', $tool);	
				$smarty->assign('order', $order);
				$smarty->display($templateDir . "/temp1/viewPInvoiceBArt.tpl");
			}
			if($stt == 2)
			{
				$orderDetailsArray = null;
				foreach ($orderDetailArr as $orderDetail) {
					$detaiArr = array();
					$orderDetails = $orderDetailTemp->getOrderDetailTempByID($context, $result, $orderDetail->getOrderdetailsDomain()->getID());
					$detaiArr[] = $orderDetail;
					$detaiArr[] = $orderDetails;
					$orderDetailsArray[] = $detaiArr;
				}
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign('orderDetailsArray', $orderDetailsArray);
				$smarty->assign('tool', $tool);	
				$smarty->assign('order', $order);
				$smarty->display($templateDir . "/temp1/viewPInvoiceEditedBArt.tpl");
			}
			if($stt == 3)
			{
				if(!Tool::isEmpty($orderDetailArr))
				{
					$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
				}
				$smarty->assign('tool', $tool);	
				$smarty->assign('order', $order);
				$smarty->display($templateDir . "/temp1/viewPInvoiceBartSumitted.tpl");
			}
		}
	}
	
	

	if($opt == "searchPI" && $permit ==true)
	{
		if(isset($_POST['dateFrom']) && isset($_POST['dateTo']) && $_POST['dateFrom'] != '' && $_POST['dateTo'] != '')
		{
			$dateFrom = $_POST['dateFrom'];
			$dateTo = $_POST['dateTo'];
		}
		else {
			$dateFrom = '0000-00-00';
			$dateTo = date('Y-m-d', time());
		}
		$consigneeID = $_SESSION['user']['ID'];
		$start=$p->findStart($configPage['limit']);
		$count = $orderServices->countAllByStatusByOrderDate($context, $result, "2,3", $dateFrom, $dateTo);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$orderList = $orderServices->findAllByStatusByOrderDateByRange($context, $result, "2,3", $start, $configPage['limit'], $dateFrom, $dateTo);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('tool', $tool);
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listPerformaInvoice.tpl");
	}

}
?>